(function($){
	$.extend($.fn,{
		dateBuilder:function(options){
			var opts=$.extend($.fn.dateBuilder.Defaults,options);
			if(opts.month>11||opts.month<0){
				alert("请检查日期输入格式！");
				return;
			}
			//alert(opts.year+"--"+opts.month);
			var db=$("table.dataBuilder"),dbItem=db.find("td"),dbCYear=db.find("i.year"),dbCMonth=db.find("i.month");
			if(db.length<1){
				var dbHtml='<table class="dataBuilder">'
							+'<caption><span class="after">></span><span class="before"><</span><span class="date"><i class="year" id="dbYear">'+opts.year+'</i>年<i class="month" id="dbMonth">'+(parseInt(opts.month)+1)+'</i>月</span></caption>'
							+'<thead><tr><th>周日</th><th>周一</th><th>周二</th><th>周三</th><th>周四</th><th>周五</th><th>周六</th></tr></thead>'
							+'<tbody>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'</tbody>'
							+'</table>';
				$("#"+opts.dateContainer).append(dbHtml);
				db=$("table.dataBuilder");
				dbItem=db.find("td");
				db.addClass(opts.custom_style).attr("id",opts.dbID);
				dateBuildFunc(opts.year,opts.month);
				opts.afterDateBuilder();
			}else{
				dbCYear.text(opts.year);
				dbCMonth.text(parseInt(opts.month)+1);
				dateBuildFunc(opts.year,opts.month);
				opts.afterDateBuilder();
			}

			// 显示选择日期盒子
			var selectDate='<div class="select_date">'
					+'<div class="year_box">'
						+'<p>选择年份</p>'
						+'<table class="select_y">'
						+'<tr><td></td><td></td></tr>'
						+'<tr><td></td><td></td></tr>'
						+'<tr><td></td><td></td></tr>'
						+'<tr><td></td><td></td></tr>'
						+'<tr><td></td><td></td></tr>'
						+'<tr><td></td><td></td></tr>'
						+'</table>'
						+'<div class="y_btn">'
							+'<span class="before" id="dbYearBefore"><</span><span class="after" id="dbYearAfter">></span></div>'
        				+'</div>'
        			+'<div class="month_box">'
        				+'<p>选择月份</p>'
        				+'<table class="select_m">'
                		+'<tr><td>1</td><td>7</td></tr>'
                		+'<tr><td>2</td><td>8</td></tr>'
                		+'<tr><td>3</td><td>9</td></tr>'
						+'<tr><td>4</td><td>10</td></tr>'
						+'<tr><td>5</td><td>11</td></tr>'
                		+'<tr><td>6</td><td>12</td></tr>'
            			+'</table>'
        			+'</div><br class="clear" />'
        			+'<div class="select_ope"><a href="javascript:void(0);" class="close">关闭</a><a href="javascript:void(0);" class="insure">确定</a></div></div>';
			
			var sDateBox=$("div.select_date");
			
			if(opts.isBind){
				$.fn.dateBuilder.Defaults.isBind=false;	
				var sd=$("span.date");
				var sdTop=sd.offset().top,sdLeft=sd.offset().left;
				var sYearTable=$("table.select_y"),sMonthTable=$("table.select_m");
				var sYearTableTD=sYearTable.find("td"),sMonthTableTD=sMonthTable.find("td");
				//alert(sdTop+"--"+sdLeft);	
				sd.bind("click",function(){
					var sYear=parseInt($("#dbYear").text());
					var sMonth=parseInt($("#dbMonth").text());
					if(sDateBox.length>0){
						sDateBox.css({
							display:"block",
							left:sdLeft-50+"px",
							top:sdTop+30+"px"	
						});		
					}else{
						$("body").append(selectDate);
						sDateBox=$("div.select_date");
						sDateBox.css({
							display:"block",
							left:sdLeft-50+"px",
							top:sdTop+30+"px"	
						});
					
					}
					
					sYearTable=$("table.select_y");
					sYearTableTD=sYearTable.find("td");	
					sMonthTable=$("table.select_m");
					sMonthTableTD=sMonthTable.find("td");
					
					//选择年月盒子重置数据
					defaultYear(sYear);
					sYearTableTD.eq(0).addClass("select");
					sMonthTableTD.removeClass();
					
					$("div.select_ope").find("a.close").bind("click",function(){
						sDateBox.css("display","none");	
					});
					
				});
				
				//绑定选择年份前后按钮
				$("#dbYearBefore").live("click",function(){
					var paraYear=parseInt(sYearTableTD.eq(0).text())-12;
					defaultYear(paraYear);
				});	
				$("#dbYearAfter").live("click",function(){
					var paraYear=parseInt(sYearTableTD.eq(0).text())+12;
					defaultYear(paraYear);
				});	
				
				sYearTableTD.live("click",function(){
					sYearTableTD.removeClass();
					$(this).addClass("select");	
				});
				
				sMonthTableTD.live("click",function(){
					sMonthTableTD.removeClass();
					$(this).addClass("select");		
				});
			}
			
			//重置年份表格
			function defaultYear(year){
				var sTDYear=$("table.select_y").find("td");	
				sTDYear.removeClass();
				var col1=year,col2=year+6;
					for(var i=0;i<sTDYear.length;i++)
					{
						if(i%2==0){
							sTDYear.eq(i).text(col1);
							col1++;	
						}else if(i%2!=0){
							sTDYear.eq(i).text(col2);	
							col2++;
						}	
					}
			}
			
			//参数年份月份，获得日期排列
			function dateBuildFunc(cYear,cMonth){
				//alert("allow");
				var newDate=new Date(cYear,cMonth,1);
				var firstDayInWeek=newDate.getDay();
				var cTotalDays=getTotalDaysInMonth(opts.year,parseInt(opts.month)); 
				var itemLen=dbItem.length;
				var curDays=1;
				dbItem.text("");
				for(i=firstDayInWeek;i<itemLen;i++){
					if(curDays>cTotalDays){
						break;	
					}else{
						
						dbItem.eq(i).text(curDays);
						curDays++;
					}	
				}
			}
			
			
			
			//获得某年某月的天数
			function getTotalDaysInMonth(cYear,cMonth){
				var leapYear=false,cTotalDays;
				if(cYear%4==0&&cYear%100!=0){
					leapYear=true;	
				}
				if(cYear%400==0){
					leapYear=true;	
				}
				switch(cMonth){
					case 0:cTotalDays=31;break;
					case 1:cTotalDays=leapYear?29:28;break;
					case 2:cTotalDays=31;break;
					case 3:cTotalDays=30;break;
					case 4:cTotalDays=31;break;
					case 5:cTotalDays=30;break;
					case 6:cTotalDays=31;break;
					case 7:cTotalDays=31;break;
					case 8:cTotalDays=30;break;
					case 9:cTotalDays=31;break;
					case 10:cTotalDays=30;break;
					case 11:cTotalDays=31;break;
				}
				return cTotalDays;
			}
			
			
			
		}		 
	});	
	
	
	
	
	var curDate=new Date(),curYear=curDate.getFullYear(),curMonth=curDate.getMonth();
	$.fn.dateBuilder.Defaults={
		year:curYear,    //默认值 当前年份 可传入参数
		month:curMonth,    //默认值 当前月份 可传入参数 0-11
		dbID:"mydb",      //默认值 生成日期表格ID 可传入参数
		dateContainer:"dateBox",   //日期容器ID  必须要有  可传入参数
		custom_style:"custom_style",    //日期客户端样式   可传入参数
		isBind:true,
		afterDateBuilder:function(){}   //date生成以后 回调函数
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	$.defUnit={
		tabsNav:function(){
			var tabsUl=$("ul.tabs_nav");
			var tabsLi=tabsUl.find("li");
			var tabsBox=$("div.tabs_box");
			var tabs=$(".tabs");
			var cIndex=0;
			tabsLi.eq(0).addClass("current");
			tabsLi.bind("mouseover",function(){
				cIndex=$(this).index();
				$(this).addClass("current").siblings().removeClass();
				if(tabsBox.length>0){
					tabs=$(this).parent().parent().siblings("div.tabs");
				}
				tabs.eq(cIndex).css({
					display:"block"					   
				}).siblings(".tabs").css({
					display:"none"			
				});
			});
			
			
		}	
	};
	
	$(function(){
		$.defUnit.tabsNav();		   
	});
	
})(jQuery);